"use strict";

/* methods */
{
    let me = {
        name: "Lucca",
        birth: 2000,
        age: function () {
            var y = new Date().getFullYear();
            return y - this.birth;
        }
    }

    me.age;
    // ƒ() {
    //     var y = new Date().getFullYear();
    //     return y - this.birth;
    // }
    me.age();
    // < 21
}

{
    let getAge = function () {
        var y = new Date().getFullYear();
        return y - this.birth;
    }

    let me = {
        name: "Lucca",
        birth: 2000,
        age: getAge
    }

    me.age();
    // 21
    getAge();
    // NaN

    let fn = me.age;
    fn();
    // NaN
}

{
    let me = {
        name: "Lucca",
        birth: 2000,
        age: function () {
            function getAgeFromBirth() {
                var y = new Date().getFullYear();
                return y - this.birth;
            }
            return getAgeFromBirth();
        }
    }

    me.age();
    // Uncaught TypeError: Cannot read property 'birth' of undefined
}

{
    let me = {
        name: "Lucca",
        birth: 2000,
        age: function () {
            var that = this;
            function getAgeFromBirth() {
                var y = new Date().getFullYear();
                return y - that.birth;
            }
            return getAgeFromBirth();
        }
    }

    me.age();
    // 21
}


/* apply */

{
    let getAge = function () {
        var y = new Date().getFullYear();
        return y - this.birth;
    }

    let me = {
        name: "Lucca",
        birth: 2000,
        age: getAge
    }

    me.age();
    // 21
    getAge.apply(me, []);
    // 21
}

Math.max.apply(null, [3, 4, 5]);
// < 5
Math.max.call(null, 3, 4, 5);
// < 5

{
    "use strict";

    let count = 0;
    let oldParseInt = parseInt;   // 保存原函数

    window.parseInt = function () {
        count += 1;
        return oldParseInt.apply(null, arguments);
    }

    parseInt("10");
    parseInt("20");
    parseInt("30");
    console.log("count =", count);
    // < count = 3

    window.parseInt = oldParseInt;
}